<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\litestore\Litestoreorder;
use app\admin\model\meet\Order;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }


        //预约订单统计
        $order_model = new Order();
        //预约订单总数
        $meet_orders = $order_model->count();
        //预约订单实付金额
        $meet_pay_amount = $order_model->where(['pay_status' => $order_model::PAY_STATUS_PAYED, 'order_status' => ['gt', $order_model::ORDER_STATUS_CANCEL]])->sum('pay_price');

        //商城订单统计
        $mall_order_model = new Litestoreorder();
        //商城订单总数
        $mall_orders = $mall_order_model->count();
        //商城订单实付金额
        $mall_pay_amount = $mall_order_model->where(['pay_status' => 20, 'order_status' => ['<>', 20]])->sum('pay_price');

        $this->view->assign([
            'totaluser' => User::count(),
            'meet_orders' => (int)$meet_orders,
            'meet_pay_amount' => (float)$meet_pay_amount,
            'mall_orders' => (int)$mall_orders,
            'mall_pay_amount' => (float)$mall_pay_amount,
            'todayusersignup' => User::whereTime('jointime', 'today')->count(),
            'todayuserlogin' => User::whereTime('logintime', 'today')->count(),
            'sevendau' => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
            'thirtydau' => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
            'threednu' => User::whereTime('jointime', '-3 days')->count(),
            'sevendnu' => User::whereTime('jointime', '-7 days')->count(),
        ]);

        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));

        return $this->view->fetch();
    }

}
